package development.app;

import iplayparticle.view.iplayparticleResources;
import state.GameConstants;
import state.GameForm;
import state.GameGlobal;
import state.GameImage;
import state.Graphics;
import state.Key;
import state.component.TextField;
import state.font.FontManager;
import state.font.ImageText;
import state.util.GameColor;
import state.util.Hashtable;
import development.Constants;

public class FrmSendGiftAlert extends GameForm {
  private String messageAlert;
  private String destination;
  private TextField txtSearch;
  private GameImage bgImage;
  private ImageText vnText;
  private GameImage buttonImage;
  private GameImage focusedButtonImage;
  private int chooseIndex;
  int alertX;
  int alertY;
  String dest;
  int eventtype;
  public void init(Hashtable parameters) {
    bgImage = new GameImage(iplayparticleResources.image_bg_popup, null, null);
    messageAlert = "Gửi tặng bạn sẽ mất phí 15$";
    destination = "Số điện thoại";
    alertX= (GameConstants.SCREEN_WIDTH - 192) / 2;
    alertY= (GameConstants.SCREEN_HEIGHT - 137) / 2;
    txtSearch = new TextField(_manager, TextField.CONSTRAINT_NUMERIC, Constants.TEXT_FIELD_STYLE);
    txtSearch.setPosition(alertX + 20 ,alertY + bgImage.getHeight() - 55);
    txtSearch.setSize(120, 18);
    txtSearch.setBorderColor(GameColor.BLACK_CODE);
    txtSearch.setFocused(true);
    vnText = FontManager.getFont(FontManager.FONT_SIZE_11);
    buttonImage = new GameImage(iplayparticleResources.image_button_nho, null, null);
    focusedButtonImage = new GameImage(iplayparticleResources.image_button_nho_focus, null, null);

    chooseIndex = 0;
    eventtype = 0;
    _manager.enableTouchPad();
  }
  public void draw(Graphics g) {
    g.drawImage(bgImage, alertX, alertY, GameConstants.TOP_LEFT_ANCHOR);
    vnText.drawParagraph(g, messageAlert,GameColor.WHITE_CODE, 180, alertX+20,alertY+20);
    vnText.drawString2(g, destination, GameColor.WHITE_CODE,alertX+20, alertY+60, GameConstants.TOP_LEFT_ANCHOR);
    txtSearch.draw(g);
    vnText.drawString2(g, "Xóa", GameColor.WHITE_CODE,  alertX +145 ,alertY + bgImage.getHeight() - 55, GameConstants.TOP_LEFT_ANCHOR);
    if(chooseIndex == 1)
      g.drawImage(focusedButtonImage, alertX + 50 ,alertY + bgImage.getHeight() - 10, GameConstants.BOTTOM_LEFT_ANCHOR);
    else
      g.drawImage(buttonImage, alertX + 50 ,alertY + bgImage.getHeight() - 10, GameConstants.BOTTOM_LEFT_ANCHOR);
    vnText.drawString2(g, "Gửi", GameColor.WHITE_CODE,  alertX + 60 ,alertY + bgImage.getHeight() -10, GameConstants.BOTTOM_LEFT_ANCHOR);
    
  }
  private void updateFocus() {
    txtSearch.setFocused(false);
    switch (chooseIndex) {
    case 0:
      txtSearch.setFocused(true);
      break;
    }
  }
  public void keyReleased(int keyCode) {
    switch (keyCode) {
    case Key.DOWN:
      chooseIndex = (chooseIndex + 1) % 2;
      updateFocus();
      break;
    case Key.UP:
      chooseIndex = (--chooseIndex < 0) ? 1 : chooseIndex;
      updateFocus();
      break;
    case Key.FIRE:
      dest = txtSearch.getText();
      dest = dest.toUpperCase().trim();
      eventtype = 1;
      GameGlobal.systemCanvas.hideAlert();
      break;
    case Key.SOFT_RIGHT:
      txtSearch.deleteLastChar();
      break;
    default:
      txtSearch.keyReleased1(keyCode);
      break;
    }
  }
  
  public void destroy() {
    messageAlert = null;
    destination = null;
    txtSearch = null;
    bgImage = null;
    vnText = null;
    buttonImage = null;
    focusedButtonImage = null;
  }
}
